package com.alu.sg.calendar;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.alu.sg.calendar.calendar.manager.CalendarManager;
import com.alu.sg.calendar.calendar.view.CollapseCalendarView;

import org.joda.time.LocalDate;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Alu on 2018/1/19.
 * version 1.0
 */

public class MainActivity extends AppCompatActivity {
    private CollapseCalendarView calendarView;
    private CalendarManager mManager;
    private JSONObject json;
    private SimpleDateFormat sdf;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        calendarView.showChinaDay(false);
        calendarView.setDateSelectListener(new CollapseCalendarView.OnDateSelect() {
            @Override
            public void onDateSelected(LocalDate date) {
                Toast.makeText(MainActivity.this, date.toDateTimeAtCurrentTime().toString("yyyy/MM/dd EE"), Toast.LENGTH_SHORT).show();
                Log.e("main", date.toDateTimeAtCurrentTime().toString());
            }
        });
    }

    private void init() {
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendarView = (CollapseCalendarView) findViewById(R.id.calendar);
        mManager = new CalendarManager(LocalDate.now(),
                CalendarManager.State.MONTH, LocalDate.now().withYear(100),
                LocalDate.now().plusYears(100));
        calendarView.showHeader();
        //月份切换监听器
        mManager.setMonthChangeListener(new CalendarManager.OnMonthChangeListener() {

            @Override
            public void monthChange(String month, LocalDate mSelected) {
            }

        });
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, 9);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        json = new JSONObject();
        try {
            for (int i = 0; i < 31; i++) {
                JSONObject jsonObject2 = new JSONObject();
                if (i % 3 != 0) {
                    jsonObject2.put("type", "课");
                }
                if (i % 3 == 0) {
                    jsonObject2.put("list", new JSONArray());
                }

                json.put(sdf.format(cal.getTime()), jsonObject2);
                cal.add(Calendar.DAY_OF_MONTH, 1);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        //设置数据显示
        calendarView.setArrayData(json);
        //初始化日历管理器
        calendarView.init(mManager);
    }
}
